﻿using System;
using System.Media;
using System.Runtime.InteropServices;
using System.Text;

namespace Client.Managers
{
    class BeepManager
    {
        public int generalWav
        {
            get => _generalWav;
            set
            {
                _generalWav = value;
                Manager.settingM.generalSetting["generalWavLength"] = _generalWav.ToString();
            }
        }
        int _generalWav;
        public int generalWavEmpty
        {
            get => _generalWavEmpty;
            set
            {
                _generalWavEmpty = value;
                Manager.settingM.generalSetting["generalWavEmptyLength"] = _generalWavEmpty.ToString();
            }
        }
        int _generalWavEmpty;
        public int kNormalWav
        {
            get => _kNormalWav;
            set
            {
                _kNormalWav = value;
                Manager.settingM.generalSetting["kNormalWavLength"] = _kNormalWav.ToString();
            }
        }
        int _kNormalWav;
        public int kNormalWavEmpty
        {
            get => _kNormalWavEmpty;
            set
            {
                _kNormalWavEmpty = value;
                Manager.settingM.generalSetting["kNormalWavEmptyLength"] = _kNormalWavEmpty.ToString();
            }
        }
        int _kNormalWavEmpty;
        public int kModelWav
        {
            get => _kModelWav;
            set
            {
                _kModelWav = value;
                Manager.settingM.generalSetting["kModelWavLength"] = _kModelWav.ToString();
            }
        }
        int _kModelWav;
        public int kModelWavEmpty
        {
            get => _kModelWavEmpty;
            set
            {
                _kModelWavEmpty = value;
                Manager.settingM.generalSetting["kModelWavEmptyLength"] = _kModelWavEmpty.ToString();
            }
        }
        int _kModelWavEmpty;
        public int kZXModelWav
        {
            get => _kZXModelWav;
            set
            {
                _kZXModelWav = value;
                Manager.settingM.generalSetting["kZXModelWavLength"] = _kZXModelWav.ToString();
            }
        }
        int _kZXModelWav;
        public int kZXModelWavEmpty
        {
            get => _kZXModelWavEmpty;
            set
            {
                _kZXModelWavEmpty = value;
                Manager.settingM.generalSetting["kZXModelWavEmptyLength"] = _kZXModelWavEmpty.ToString();
            }
        }
        int _kZXModelWavEmpty;
        public int tickWav
        {
            get => _tickWav;
            set
            {
                _tickWav = value;
                Manager.settingM.generalSetting["tickWavLength"] = _tickWav.ToString();
            }
        }
        int _tickWav;
        public int tickWavEmpty
        {
            get => _tickWavEmpty;
            set
            {
                _tickWavEmpty = value;
                Manager.settingM.generalSetting["tickWavEmptyLength"] = _tickWavEmpty.ToString();
            }
        }
        int _tickWavEmpty;

        SoundPlayer _player;

        public BeepManager()
        {
            _generalWav = int.Parse(Managers.Manager.settingM.GetGeneralValue("generalWavLength"));
            _generalWavEmpty = int.Parse(Managers.Manager.settingM.GetGeneralValue("generalWavEmptyLength"));
            _kNormalWav = int.Parse(Managers.Manager.settingM.GetGeneralValue("kNormalWavLength"));
            _kNormalWavEmpty = int.Parse(Managers.Manager.settingM.GetGeneralValue("kNormalWavEmptyLength"));
            _kModelWav = int.Parse(Managers.Manager.settingM.GetGeneralValue("kModelWavLength"));
            _kModelWavEmpty = int.Parse(Managers.Manager.settingM.GetGeneralValue("kModelWavEmptyLength"));
            _kZXModelWav = int.Parse(Managers.Manager.settingM.GetGeneralValue("kZXModelWavLength"));
            _kZXModelWavEmpty = int.Parse(Managers.Manager.settingM.GetGeneralValue("kZXModelWavEmptyLength"));
            _tickWav = int.Parse(Managers.Manager.settingM.GetGeneralValue("tickWavLength"));
            _tickWavEmpty = int.Parse(Managers.Manager.settingM.GetGeneralValue("tickWavEmptyLength"));
            
            _player = new SoundPlayer();
        }


        public void PlayBeep(YYBeep.Beep bp)
        {
            _player.Stop();
            _player.Stream = YYBeep.WavHelper.GetWavStream(new YYBeep.Beep[] { bp });
            _player.Play();
        }
        public void PlayBeeps(YYBeep.Beep[] bpList)
        {
            if (bpList == null)
                return;

            _player.Stop();
            _player.Stream = YYBeep.WavHelper.GetWavStream(bpList);
            _player.Play();
        }

        public void PlayXBorder()
        {
            WavPlayer.PlaySync(@"sounds\xborder.wav");
        }
        public void PlayYBorder()
        {
            WavPlayer.PlaySync(@"sounds\yborder.wav");
        }
        public void PlayNoItem()
        {
            WavPlayer.Play(@"sounds\noitem.wav");
        }
        public void PlaySuccessed()
        {
            WavPlayer.Play(@"sounds\successed.wav");
        }
        public void PlayBack()
        {
            WavPlayer.Play(@"sounds\back.wav");
        }
        public void PlayAddedZXG()
        {
            WavPlayer.Play(@"sounds\addedzxg.wav");
        }
    }

    public static class WavPlayer
    {
        [DllImport("winmm.dll", SetLastError = true)]
        static extern bool PlaySound(string pszSound, UIntPtr hmod, uint fdwSound);
        [DllImport("winmm.dll", SetLastError = true)]
        static extern long mciSendString(string strCommand,
            StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
        [DllImport("winmm.dll")]
        private static extern long sndPlaySound(string lpszSoundName, long uFlags);

        [Flags]
        public enum SoundFlags
        {
            /// <summary>play synchronously (default)</summary>
            SND_SYNC = 0x0000,
            /// <summary>play asynchronously</summary>
            SND_ASYNC = 0x0001,
            /// <summary>silence (!default) if sound not found</summary>
            SND_NODEFAULT = 0x0002,
            /// <summary>pszSound points to a memory file</summary>
            SND_MEMORY = 0x0004,
            /// <summary>loop the sound until next sndPlaySound</summary>
            SND_LOOP = 0x0008,
            /// <summary>don’t stop any currently playing sound</summary>
            SND_NOSTOP = 0x0010,
            /// <summary>Stop Playing Wave</summary>
            SND_PURGE = 0x40,
            /// <summary>don’t wait if the driver is busy</summary>
            SND_NOWAIT = 0x00002000,
            /// <summary>name is a registry alias</summary>
            SND_ALIAS = 0x00010000,
            /// <summary>alias is a predefined id</summary>
            SND_ALIAS_ID = 0x00110000,
            /// <summary>name is file name</summary>
            SND_FILENAME = 0x00020000,
            /// <summary>name is resource name or atom</summary>
            SND_RESOURCE = 0x00040004
        }

        public static void Play(string strFileName)
        {
            PlaySound(strFileName, UIntPtr.Zero,
               (uint)(SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC | SoundFlags.SND_NOSTOP));
        }

        public static void PlaySync(string strFileName)
        {
            PlaySound(strFileName, UIntPtr.Zero,
               (uint)(SoundFlags.SND_FILENAME | SoundFlags.SND_SYNC));
        }
        public static void mciPlay(string strFileName)
        {
            string playCommand = "open " + strFileName + " type WAVEAudio alias MyWav";
            mciSendString(playCommand, null, 0, IntPtr.Zero);
            mciSendString("play MyWav", null, 0, IntPtr.Zero);

        }
        public static void sndPlay(string strFileName)
        {
            sndPlaySound(strFileName, (long)SoundFlags.SND_SYNC);
        }
    }
}



